<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\HotsalesRecommend;

/**
 * HotsalesRecommendSearch represents the model behind the search form about `common\models\HotsalesRecommend`.
 */
class HotsalesRecommendSearch extends HotsalesRecommend
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['hotsales_recommend_id', 'goods_id', 'display', 'sort'], 'integer'],
            [['goods_name', 'recommend_starttime', 'recommend_endtime', 'add_time'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = HotsalesRecommend::find();

        if(isset($params['goods_name']))
        {
            $params['HotsalesRecommendSearch']['goods_name'] = $params['goods_name'];
        }elseif(isset($params['goods_id'])){
            $params['HotsalesRecommendSearch']['goods_id'] = intval($params['goods_id']);
        }

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'goods_id' => $this->goods_id,
            'display' => $this->display,
            'sort' => $this->sort,
            'recommend_starttime' => $this->recommend_starttime,
            'recommend_endtime' => $this->recommend_endtime,
        ]);

        $query->andFilterWhere(['like', 'goods_name', $this->goods_name]);

        return $dataProvider;
    }
}
